package utils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: util
 * @description: 时间分割
 * @author: zhouliangliang
 * @create: 2019-03-08 14:29
 **/
public class DateSplit {


    /**
     * 获取从开始时间到截止时间每interval分隔的时刻
     * @param date
     * @param interval
     * @return
     */
    private static List<Date> separateDate(Date date, int interval) {
        Date start = dayStartDate(date);//转换为天的起始date
        Date nextDayDate = nextDay(start);//下一天的date

        List<Date> result = new ArrayList<Date>();
        while (start.compareTo(nextDayDate) <= 0) {
            result.add(start);
            start = addMins(start, interval);
        }
        return result;
    }

    /**
     * 给当前时间加上分钟数
     * @param start
     * @param offset
     * @return
     */
    private static Date addMins(Date start, int offset) {
        Calendar c = Calendar.getInstance();
        c.setTime(start);
        c.add(Calendar.MINUTE, offset);
        return c.getTime();
    }

    /**
     * 获得给定日期的下一天
     * @param start
     * @return
     */
    private static Date nextDay(Date start) {
        Calendar c = Calendar.getInstance();
        c.setTime(start);
        c.add(Calendar.DATE, 1);
        return c.getTime();
    }

    /**
     * 获得某一天的开始时间
     * @param date
     * @return
     */
    private static Date dayStartDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 给时间加(减)响应的秒数
     * @param date
     * @param interval
     * @return
     */
    private static Date addSecond(Date date , int interval) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.SECOND, interval);
        return c.getTime();
    }

    /**
     * 给时间加(减)响应的毫秒数
     * @param date
     * @param interval
     * @return
     */
    private static Date addMillSecond(Date date, int interval) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MILLISECOND, interval);
        return c.getTime();
    }


    /**
     * 把开始时刻和结束时刻写入Data中
     * @param date
     * @param interval
     * @return
     */
    public static List<Data> dateSplit(Date date, int interval) {
        List<Date> ds = separateDate(date, interval);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        List<Data> result = new LinkedList<Data>();
        for(int i = 0; i < ds.size()-1 ; i++) {
            Data data = new Data();
            data.setStart(sdf.format(ds.get(i)));
            data.setEnd(sdf.format(addMillSecond(ds.get(i+1),-1)));
            result.add(data);
        }
        return result;
    }

    public static void show(String date, int interval) throws Exception {
        Date dd = new SimpleDateFormat("yyyy-MM-dd").parse(date);
        List<Data> result = DateSplit.dateSplit(dd, interval);
        for(Data data : result) {
            System.out.println(data);
        }
    }


    public static void main(String[] args) throws Exception{
        String [] data = new String [] {"2019-03-05"};

        for(String ss : data) {
            DateSplit.show(ss, 5);
        }
    }

    static class Data{
        private String start;
        private String end;

        public String getStart() {
            return start;
        }

        public void setStart(String start) {
            this.start = start;
        }

        public String getEnd() {
            return end;
        }

        public void setEnd(String end) {
            this.end = end;
        }

        @Override
        public String toString() {
            return this.start + " :  " + this.end;
        }
    }
}
